// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
var g = require('Global')

cc.Class({
    extends: cc.Component,

    properties: {
        itemSprites: {
            default: [],
            type: cc.Node
        },
        prefab: cc.Prefab,
        blockContainer: cc.Layout,
        selectContainer: cc.Layout,
        currentSelect: cc.Sprite,
        winPanel: cc.Node,
        data: {
            default: null,
            type: g.BlockItem
        }
    },

    onLoad() {
        this.data = new g.BlockItem().init(9, 9, 3, 3, "761030025350008107020007034009006378003279500570300902195760000832400760647010250")

        console.log(">>", this.blockContainer.node.width, this.blockContainer.node.height)
        this.blockContainer.spacingX = this.blockContainer.spacingY = 2
        var padding = this.blockContainer.padding = this.blockContainer.spacingX * 2

        var w = (this.blockContainer.node.width -
            this.data.col * this.blockContainer.spacingX - padding * 2
        ) / this.data.col;

        this.blockContainer.spacingY = this.blockContainer.spacingX

        this.blockContainer.cellSize = new cc.Size(w, w)
        var height = (w + this.blockContainer.spacingY) * this.data.row + padding * 2
        this.blockContainer.node.height = height

        this.blockContainer.node.removeAllChildren()

        for (var i = 0; i < this.data.values.length; i++) {
            var item = cc.instantiate(this.prefab)
            var data = this.data.values[i]
            //console.log("item:", data)

            item.getComponent('BlockItemTemplate').init({
                id: i,
                filledValue: data
            })
            this.itemSprites.push(item)
            this.blockContainer.node.addChild(item)
            item.on(cc.Node.EventType.TOUCH_END, this.onTouchBlock, this)
        }
        this.node.on('onFillBlockClick', this.onFillCmd, this)
    },
    onTouchBlock: function (event) {
        // console.log(event)
        if (event.target.getComponent('BlockItemTemplate').selectable == false) {
            console.log("元素不可点")
            return
        }

        if (this.currentSelect) {
            this.currentSelect.getComponent('BlockItemTemplate').setSelected(false)
        }
        this.currentSelect = event.target
        this.currentSelect.getComponent('BlockItemTemplate').setSelected(true)
    },
    onFillCmd: function (event) {
        console.log(event)
        if (!this.currentSelect) return
        var data = event.getUserData()
        var conflict = []
        if (data.value == "X") {
            var sprite = this.currentSelect.getComponent('BlockItemTemplate').updateValue("")
            this.data.fillBlock(sprite.id, 0)
        } else {
            var sprite = this.currentSelect.getComponent('BlockItemTemplate').updateValue(data.value)
            conflict = this.data.fillBlock(sprite.id, data.value)
        }

        //如果还未填完,则显示当前重复项.否则显示所有重复项.
        var fullConflict = this.data.checkWin()
        if (fullConflict == null) fullConflict = conflict;
        else if (fullConflict.length == 0) {
            console.log("YOU WIN!!!")
            this.winPanel.opacity = 255
            this.winPanel.on(cc.Node.EventType.TOUCH_END, this.onWinClick, this)
            return
        }

        for (var i = 0; i < this.itemSprites.length; i++) {
            this.itemSprites[i].getComponent('BlockItemTemplate').setConflict(false)
        }
        for (var i in fullConflict) {
            this.itemSprites[fullConflict[i].i].getComponent('BlockItemTemplate').setConflict(true)
        }
    },
    onWinClick: function (event) {
        this.winPanel.opacity = 0
        this.onLoad()
        this.winPanel.off(cc.Node.EventType.TOUCH_END)
    },
    start() {

    },
});
